using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Principal;
using System.Threading;
using System.Security;

namespace Decav.Gatsb.Security
{
    /// <summary>
    /// The principal that holds roles for Gatsb role based security.
    /// </summary>
    public class GatsbPrincipal : IPrincipal
    {
        public GatsbPrincipal(GatsbIdentity identity)
        {
            if (identity == null)
                throw new ArgumentNullException("identity");

            _identity = identity;
        }

        private GatsbIdentity _identity;

        /// <summary>
        /// Gets if the <see cref="GatsbPrincipal"/> on the current thread exists and is
        /// authenticated.
        /// </summary>
        public static bool IsAuthenticated
        {
            get
            {
                if (Thread.CurrentPrincipal is GatsbPrincipal)
                    return Current.Identity.IsAuthenticated;

                return false;
            }
        }

        /// <summary>
        /// Gets the current Gatsb principal for the current thread.
        /// </summary>
        public static GatsbPrincipal Current
        {
            get
            {
                GatsbPrincipal p = Thread.CurrentPrincipal as GatsbPrincipal;
                if (p == null)
                    throw new SecurityException("The currently authenticated user does not have a GatsbIdentity.");

                return p;
            }
        }

        /// <summary>
        /// Gets the identity of the current user.
        /// </summary>
        public GatsbIdentity Identity
        {
            get
            {
                return _identity;
            }
        }

        #region IPrincipal Members

        IIdentity IPrincipal.Identity
        {
            get
            {
                return _identity;
            }
        }

        public bool IsInRole(string role)
        {
            return false;
        }

        #endregion
    }
}
